/*
 * 
 * */
 
 Ext.define('MobileTest.view.window.KPIEditWindow',{
 	extend:'Ext.window.Window', 	
 	width:800,
 	height:500,
 	modal:true,
 	act:'',
 	rec:null,
 	title:'新建业务模版', 	
	initComponent : function() {
		
		var me = this;
		
		var tvstore = Ext.create('Ext.data.TreeStore', {
							autoLoad:false,
							fields:['id', 'code', 'text', 'desc', 'parentId'],
							proxy: {
            					type: 'ajax',
            					url: 'KpiTree'            					
        					},
        					 root: {
            					text: '.',
            					id: '',     
            					expanded: true
        					}
						});
					 	
		var editfrm= Ext.create('Ext.form.Panel', {    			
    					bodyPadding: 5,    				
    					region:'north',
    					layout: 'anchor',
    					defaults: {
        					anchor: '95%',        					
    					},
    					 fieldDefaults: {
            				labelAlign: 'right'            				
        				},
    					defaultType: 'textfield',
    					items: [ {
        							fieldLabel: '模版名称',
        							name: 'name',
        							anchor:'50%',
        							allowBlank: false
    						     },{
        							xtype     : 'textareafield',
        							grow      : true,
        							name      : 'content',
        							fieldLabel: '业务',
        							anchor    : '50%'
    							},{
        							xtype: 'hiddenfield',
        							name: 'id',
        							value: 0
    							}]} );
		
		Ext.applyIf(me, {
			layout:'border',  
			items:[			
				{
					xtype:'treepanel',
					region:'center',
					title: '选择业务指标',    				
    				store: tvstore,
    				rootVisible: false,
    				listeners:{checkchange:function( node,  checked,  eOpts){
    					    					
    					var form=me.child('form').getForm();
    					var tree=me.child('treepanel');
    					
            			var checkedNodes = tree.getChecked();
						var sel='';
								
						for(var i=0;i<checkedNodes.length;i++){		
										
								sel+=checkedNodes[i].data.id+';';
						}
						
						form.findField('content').setValue(sel);
    					
    					}
    				}
				},editfrm
			],
 			dockedItems:[ {
					xtype : 'toolbar',
					dock : 'top',
					items : [ {
							text : '保存',
							tooltip : '保存',
							iconCls : 'add',
							handler : function() {
							
								var form = me.down('form').getForm();
            					if (form.isValid()) {
            						
                					form.submit({
                						url: 'submitKqiGroup',
    									//params: {
        								//	content: sel
    									//},
                    					success: function(form, action) {
                       						Ext.Msg.alert('Success', action.result.msg);
                       						me.close();                       						
                    					},
                    					failure: function(form, action) {
                        					Ext.Msg.alert('Failed', action.result.msg);
                    					}
                					});
            					}							
							
							}
						}, {
							text : '关闭',
							tooltip : '关闭窗口',
							iconCls : 'add',
							handler : function() {
									me.close();
							}
					}]
			}]
		});
		me.callParent(arguments);
		
		me.on('beforerender',me.on_beforerender);
			
	},
	on_beforerender:function(me, eOpts){
		
		var rec=me.rec;
		
		if(rec!=null){
			
			var form=me.child('form').getForm();
			
			form.findField('id').setValue(rec.data.id);
			form.findField('name').setValue(rec.data.name);
			
			form.findField('content').setValue(rec.data.content);
						
		}
		
	}
	
 });